package com.example.manualManagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.manualManagement.entity.Department;
import com.example.manualManagement.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DepartmentService {

    @Autowired
    private DepartmentMapper dpMapper;

    public List<Department> findByName(String dpName) {
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.eq("dpName",dpName);

        List<Department> dp = dpMapper.selectList(wrapper);
        return dp;
    }

    public List<Department> queryPage( Integer page) {
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRows = pageSize*(pageNow-1);
        long jiewei=startRows+ pageSize;
        long n=dpMapper.selectCount(null);
        if(jiewei>n) {
            jiewei=n;
        }
        //if(startRows<=jiewei) {  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
        List<Department> userPage=dpMapper.selectList(null);
        return userPage.subList( startRows,(int)jiewei);

    }

    public long getRowCount() {
        return dpMapper.selectCount(null);
    }

    public int insertUser(Department dp) {

        return dpMapper.insert(dp);
    }

    public List<Department> ListUser(){
        return dpMapper.selectList(null);
    }

    public int Update(Department dp){
        return dpMapper.updateById(dp);
    }

    public int delete(int dpId){

        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.eq("dpId",dpId);
        return dpMapper.delete(wrapper);
    }

}
